﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfCalculator
{
    public abstract class CalculatorOperator
    {
        public abstract double Operator(string text, double secondNum);
        public abstract void Number();
    }

    public class Operation : CalculatorOperator
    {
        public Operation()
        {
            _Number = "0";
            _Expression = "";

            ZeroActionCommand = new DelegateCommand(ZeroAction);
            OneActionCommand = new DelegateCommand(OneAction);
            TwoActionCommand = new DelegateCommand(TwoAction);
            ThreeActionCommand = new DelegateCommand(ThreeAction);
            FourActionCommand = new DelegateCommand(FourAction);
            FiveActionCommand = new DelegateCommand(FiveAction);
            SixActionCommand = new DelegateCommand(SixAction);
            SevenActionCommand = new DelegateCommand(SevenAction);
            EightActionCommand = new DelegateCommand(EightAction);
            NineActionCommand = new DelegateCommand(NineAction);
            PlusActionCommand = new DelegateCommand(PlusAction);
            MinusActionCommand = new DelegateCommand(MinusAction);
            MultiplyActionCommand = new DelegateCommand(MultiplyAction);
            DivideActionCommand = new DelegateCommand(DivideAction);
            EqualActionCommand = new DelegateCommand(EqualAction);
            CActionCommand = new DelegateCommand(CAction);
            CEActionCommand = new DelegateCommand(CEAction);
            NegateActionCommand = new DelegateCommand(NegateAction);
            RemoveOneActionCommand = new DelegateCommand(RemoveOneAction);
            AddPointActionCommand = new DelegateCommand(AddPointAction);
            SquareActionCommand = new DelegateCommand(SquareAction);
            SqrtActionCommand = new DelegateCommand(SqrtAction);
            DerivativeActionCommand = new DelegateCommand(DerivativeAction);
            AbsActionCommand = new DelegateCommand(AbsAction);
        }

        public string _Number
        {
            get => GetValue<string>();
            set => SetValue(value);
        }

        public string _Expression
        {
            get => GetValue<string>();
            set => SetValue(value);
        }

        public bool IsNum { get; set; }

        public override void Number()
        {
            
        }

        public override double Operator(string text, double secondNum)
        {
            char[] chars = new[] { ' ' };
            string[] newText = null;
            if (text != "")
            {
                newText = text.Split(chars, StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                return double.Parse(_Number);
            }

            var returnNum = 0d;

            switch (newText.Last())
            {
                case "+":
                    returnNum = double.Parse(newText[0]).Plus(secondNum);
                    break;
                case "-":
                    returnNum = double.Parse(newText[0]).Minus(secondNum);
                    break;
                case "×":
                    returnNum = double.Parse(newText[0]).Multiply(secondNum);
                    break;
                case "÷":
                    returnNum = double.Parse(newText[0]).Divide(secondNum);
                    break;
                case "=":
                    return double.Parse(Number);

            }

            return returnNum;
        }
    }
}
